package com.daasuu.easinginterpolator;

import com.daasuu.ei.Ease;
import com.daasuu.ei.EasingInterpolator;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.StackLayout;
import ohos.agp.utils.Color;
import ohos.agp.utils.Point;
import ohos.app.Context;
import ohos.agp.render.Paint;
import ohos.agp.render.Canvas;

public class EasingGraphView extends StackLayout{

    private Paint mPaint = new Paint();
    private Paint mFramePaint = new Paint();
    private Paint mTextPaint = new Paint();

    private EasingInterpolator easingInterpolator;
    private float mAdjustTextMesureY = -1;

    private float dpSize;


    private final int loopSize = 100;

    public EasingGraphView(Context context) {
        this(context, null, null);
    }

    public EasingGraphView(Context context, Ease ease) {
        this(context, null, null);
        easingInterpolator = new EasingInterpolator(ease);

    }

    public EasingGraphView(Context context, AttrSet attrs) {
        this(context, attrs, null);
    }

    //AttributeSet:与XML中的标记相关联的属性集合文件(安卓)
    public EasingGraphView(Context context, AttrSet attrs, String defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mPaint.setColor(Color.BLUE);
        mPaint.setAntiAlias(true);

        mFramePaint.setColor(Color.BLUE);
        mFramePaint.setAntiAlias(true);
        mFramePaint.setStyle(Paint.Style.STROKE_STYLE);

        dpSize = AttrHelper.vp2px(1, context);


        mTextPaint.setTextSize((int) AttrHelper.vp2px(12, context));
        mAdjustTextMesureY = mTextPaint.getTextSize();

        DrawTask task = new DrawTask() {
            @Override
            public void onDraw(Component component, Canvas canvas) {
                dispatchDraw(canvas);
            }
        };
        addDrawTask(task);
    }


    private void dispatchDraw(Canvas canvas) {

        if (easingInterpolator == null) {
            easingInterpolator = new EasingInterpolator(Ease.ELASTIC_IN_OUT);
        }

        int xWidth = (int) mTextPaint.measureText(easingInterpolator.getEase().name());
        float x = (loopSize * dpSize - xWidth) / 2f;
        if (x < 0) {
            x = 0;
        }
        canvas.drawText(mTextPaint, easingInterpolator.getEase().name(), x, mAdjustTextMesureY);


        Point points[] = new Point[loopSize];
        for (int i = 0; i < loopSize; i++) {
            points[i] = new Point(i * dpSize, (loopSize - easingInterpolator.getInterpolation((float) i / loopSize) * loopSize) * dpSize);
        }

        canvas.save();
        {
            canvas.translate(0, 40 * dpSize);
            for (int i = 0, n = loopSize - 1; i < n; i++) {
                canvas.drawLine(new Point(points[i].getPointX(), points[i].getPointY()),
                        new Point(points[i + 1].getPointX(), points[i + 1].getPointY()), mPaint);
            }
            canvas.drawRect(0, 0, loopSize * dpSize, loopSize * dpSize, mFramePaint);
        }
        canvas.restore();

    }
}